<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/7
 * Time: 15:55
 */

namespace app\api\model;


class Theme extends BaseModel
{
    protected $hidden = ['update_time', 'delete_time', 'topic_img_id', 'head_img_id'];
    // 一对一关系中，有外键的使用belongsTo，无外键的使用hasOne
    public function topicImg() {
        return $this->belongsTo('Image', 'topic_img_id', 'id');
    }
    public function headImg() {
        return $this->belongsTo('Image', 'head_img_id', 'id');
    }
    // theme关联theme_product
    public function products() {
        return $this->belongsToMany('Product', 'theme_product',
            'product_id', 'theme_id');
    }
    public static function getThemeByIDs($ids) {
        $ids = explode(',', $ids);
        $theme = self::with('topicImg,headImg')->select($ids);
        return $theme;
    }
    public static function getThemeWithProducts($id) {
        $theme = self::with('products,topicImg,headImg')
            ->find($id);
        return $theme;
    }
}